package com.gitee.sop.websiteserver.service;

import com.alibaba.fastjson.JSONObject;
import com.gitee.fastmybatis.core.PageInfo;
import com.gitee.fastmybatis.core.query.Query;
import com.gitee.fastmybatis.core.query.Sort;
import com.gitee.fastmybatis.core.util.MapperUtil;
import com.gitee.sop.file.AliOssProperties;
import com.gitee.sop.file.AliyunOssService;
import com.gitee.sop.message.util.DateUtils;
import com.gitee.sop.message.util.ObjectUtil;
import com.gitee.sop.mongodb.MongoDbUtil;
import com.gitee.sop.mongodb.MongoQuery;
import com.gitee.sop.websiteserver.constants.TemplateStatus;
import com.gitee.sop.websiteserver.controller.param.ResourcePageParam;
import com.gitee.sop.websiteserver.entity.Resource;
import com.gitee.sop.websiteserver.mapper.ResourceMapper;
import com.gitee.sop.websiteserver.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author: Admin
 * @date: 2021/4/14
 * @description:
 * @copyright: 重庆万蓝兴科技有限公司
 */
@Service
public class ResourceService extends BaseServiceImpl {

    @javax.annotation.Resource
    private ResourceMapper resourceMapper;

    @javax.annotation.Resource
    private MongoDbUtil mongoDbUtil;

    private AliyunOssService aliyunOssService;


    /**
     * 分页获取资源信息
     *
     * @param param 参数
     * @return
     */
    public PageInfo<Resource> page(ResourcePageParam param) {
        Query query = Query.build(param);
        query.orderby("created_time", Sort.DESC);
        PageInfo<Resource> pageInfo = MapperUtil.query(resourceMapper, query);
        return pageInfo;
    }

    /**
     * 刪除指定的资源信息
     *
     * @param uuid  资源uuid
     * @param appId 应用ID
     */
    public void delete(String uuid, String appId) {
        Query query = new Query();
        query.eq("uuid", uuid);
        query.eq("app_id", appId);
        Resource resource = resourceMapper.getByQuery(query);
        if (Objects.nonNull(resource)) {
            resourceMapper.delete(resource);
            JSONObject jsonObject = checkAndGetConfig(appId);
            if (Objects.nonNull(jsonObject)) {
                String fileName = resource.getFileName();
                Date createdTime = resource.getCreatedTime();
                String dateToStr = DateUtils.parseDateToStr("yyyy/MM/dd", createdTime);
                String ossKey = dateToStr + "/" + fileName;
                aliyunOssService.delete(ossKey);
            }
        }
    }


    /**
     * 校验文件模板是否可正常使用
     *
     * @param appId 应用ID
     * @return
     */
    public JSONObject checkAndGetConfig(String appId) {
        List<MongoQuery> mongoQueries = new ArrayList<>();
        mongoQueries.add(new MongoQuery("appId", appId));
        mongoQueries.add(new MongoQuery("status", TemplateStatus.normal.getCode()));
        JSONObject jsonObject = mongoDbUtil.queryOne(getCollectionName(), mongoQueries, JSONObject.class);
        if (Objects.nonNull(jsonObject)) {
            String templateType = jsonObject.getString("templateType");
            Date startTime = jsonObject.getDate("startTime");
            Date endTime = jsonObject.getDate("endTime");
            Date currDate = new Date();
            if (currDate.after(startTime) && currDate.before(endTime)) {
                AliOssProperties aliOssProperties = new AliOssProperties();
                ObjectUtil.switchAllFieldValue(aliOssProperties, jsonObject);
                this.aliyunOssService = new AliyunOssService(aliOssProperties);
                return jsonObject;
            }
        }
        return null;
    }

    @Override
    protected String getCollectionName() {
        return "filesystemConfig";
    }
}
